package org.shoukaiseki.commons.utils.mysql;

import com.mysql.cj.MysqlType;

import java.sql.Types;

/**
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  18-10-23 下午5:32<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
public class MysqlDataTypeUtils {

    /**
     * 返回 mybatis 中的jdbcType 和 对应 java数据类型
     * @param type  metaData.getColumnType(i) 获得
     * @return {columnType,jdbcType,mybatisJdbcType}
     */
    public static String[] getJdbcTypeFromColumnType(int type,Integer columnDisplaySize){

        String columnType = "java.lang.String";
        String jdbcType = "VARCHAR";
        String mybatisJdbcType=null;
        switch (type){
            case  Types.BIGINT:
                jdbcType = MysqlType.BIGINT.getName();
                columnType="java.lang.Long";
                break;
            case  Types.DECIMAL:
                jdbcType = MysqlType.DECIMAL.getName();
                columnType="java.math.BigDecimal";
                break;
            case  Types.TINYINT:
                jdbcType = MysqlType.TINYINT.getName();
                columnType="java.lang.Short";
                break;
            case  Types.INTEGER:
                jdbcType = MysqlType.INT.getName();
                mybatisJdbcType="INTEGER";

                if(columnDisplaySize!=null&&columnDisplaySize>10){
                    columnType="java.lang.Long";
                }else{
                    columnType="java.lang.Integer";
                }
                break;
            case  Types.SMALLINT:
                jdbcType = MysqlType.SMALLINT.getName();
                columnType="java.lang.Integer";
                break;
            case  Types.BIT:
                jdbcType = MysqlType.BIT.getName();
                columnType="java.lang.Short";
                break;
            case  Types.DATE:
                jdbcType = MysqlType.DATE.getName();
                columnType="java.util.Date";
                break;
            case  Types.TIME:
                jdbcType = MysqlType.TIME.getName();
                columnType="java.util.Date";
                break;
            case  Types.TIMESTAMP:
                jdbcType = MysqlType.TIMESTAMP.getName();
                columnType="java.util.Date";
                break;
            case  Types.DOUBLE:
                jdbcType = MysqlType.DOUBLE.getName();
                columnType="java.math.BigDecimal";
                break;
            case  Types.REAL:
                jdbcType = MysqlType.FLOAT.getName();
                columnType="java.math.BigDecimal";
                break;
            case  Types.VARCHAR:
                break;
            default:
                break;
        }
        if(mybatisJdbcType==null){
            mybatisJdbcType=jdbcType;
        }

        return new String[]{columnType,jdbcType,mybatisJdbcType};
    }

}

